﻿namespace ap2.Phone.Core.ViewModels
{
    using System;
    using Microsoft.Practices.Prism.ViewModel;
    using Microsoft.Practices.Prism;
    using Microsoft.Practices.Prism.Interactivity.InteractionRequest;
    using ap2.Phone.Core.Services;
    using Microsoft.Phone.Shell;

    public abstract class ViewModelBase : NotificationObject, IDisposable
    {
        #region Attributes
        private readonly INavigationService navigationService;
        private bool disposed;
        #endregion

        #region Properties

        #endregion

        #region Ctor
        protected ViewModelBase(INavigationService navigationService)
        {
            PhoneApplicationService.Current.Deactivated += this.OnDeactivated;
            PhoneApplicationService.Current.Activated += this.OnActivated;

            this.navigationService = navigationService;

        }

        ~ViewModelBase()
        {
            this.Dispose();
        } 
        #endregion

        #region IDisposable
        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }
        #endregion

        #region Methods
        public INavigationService NavigationService
        {
            get { return this.navigationService; }
        }

        public virtual void IsBeingDeactivated()
        {

        }

        public abstract void IsBeingActivated();

        protected virtual void Dispose(bool disposing)
        {
            if (this.disposed)
            {
                return;
            }

            if (disposing)
            {
                PhoneApplicationService.Current.Deactivated -= this.OnDeactivated;
                PhoneApplicationService.Current.Activated -= this.OnActivated;
            }

            this.disposed = true;
        }

        private void OnDeactivated(object s, DeactivatedEventArgs e)
        {
            this.IsBeingDeactivated();
        }

        private void OnActivated(object s, ActivatedEventArgs e)
        {
            this.IsBeingActivated();
        } 
        #endregion

    }
}
